package com.example.bookshop.controller;

import com.example.bookshop.bean.BookTypeBean;
import com.example.bookshop.bean.PageBean;
import com.example.bookshop.bean.ResultBean;
import com.example.bookshop.dao.BookTypeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BookTypeController {
    @Autowired
    private BookTypeDao bookTypeDao;
    /**
     * 添加类型
     *
     * @param typeName   类型名称
     * @param typeDepict 类型描述
     * @return {@link ResultBean}
     */
    @RequestMapping("add_type")
    public ResultBean addType(String typeName , String typeDepict){
        ResultBean result = new ResultBean();
        BookTypeBean type = new BookTypeBean();
        type.setTypeName(typeName);
        type.setTypeDepict(typeDepict);
        if (bookTypeDao.insertType(type) == 1){
            result.setCode(1);
            result.setMsg("添加成功!");
        }else {
            result.setCode(-1);
            result.setMsg("添加失败!");
        }
        return result;
    }

    /**
     * 查找所有类型
     *
     * @return {@link ResultBean}
     */
    @RequestMapping("/find_type")
    public ResultBean findType(){
        ResultBean result = new ResultBean();
        List<BookTypeBean> types = bookTypeDao.selectTypes();
        result.setCode(1);
        result.setData(types);
        return result;
    }

    @RequestMapping("/find_type_page")
    /**
     * 分页查询
     *
     * @return {@link ResultBean}
     */
    public ResultBean findTypePage(Integer pageNow){
        //
        PageBean page = new PageBean();
        page.setRowCount(bookTypeDao.getRowCount());//总行数
        page.setPageCount(page.getRowCount()%page.getPageSize()==0
                          ? page.getRowCount()/page.getPageSize()
                          : page.getRowCount()/page.getPageSize()+1);//总页数
        //当前页
        page.setPageNow(pageNow==null?1:
                        pageNow<1?1:
                        pageNow>page.getPageCount()?page.getPageCount():pageNow);
       ResultBean result = new ResultBean();
        List<BookTypeBean> types = bookTypeDao.selectTypesPage(page);
        result.setCode(1);

        //将查询的信息和分页信息封装
        Map<String,Object> map= new HashMap<>();
        map.put("types",types);
        map.put("page",page);

        result.setData(map);
        return result;
    }

    /**
     * 删除图书类型数据
     *
     * @param id id
     * @return {@link ResultBean}
     */
    @RequestMapping("/drop_type")
    public ResultBean dropType(Integer id){
        ResultBean result = new ResultBean();
        if (bookTypeDao.deleteType(id)==1){
            result.setCode(1);
            result.setMsg("删除成功!");
        }else {
            result.setCode(-1);
            result.setMsg("删除失败!");
        }
        return result;
    }

    /**
     * 更新类型
     *
     * @param type 类型
     * @return {@link ResultBean}
     */
    @RequestMapping("/edit_type")
    public ResultBean editType(BookTypeBean type){
        ResultBean result = new ResultBean();
        if (bookTypeDao.updateType(type)>=1){
            result.setCode(1);
            result.setMsg("修改成功!");
        }else {
            result.setCode(1);
            result.setMsg("修改失败!");
        }
        return result;
    }
}
